﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SBLib.Common
{
    public static class DateFunc
    {

        /// <summary>
        /// Get the future working date
        /// </summary>
        /// <param name="prmDatCtr">Number of day</param>
        /// <param name="prmDate">From Date</param>
        /// <returns></returns>
        public static DateTime GenerateNextBusinessDay(int prmDatCtr, DateTime prmDate)
        {
            try
            {
                if (prmDate == DateTime.MinValue) return DateTime.Now.Date;

                for (int i = 1; i <= prmDatCtr; i++)
                {
                    if (prmDate.DayOfWeek.ToString().ToUpper() == "FRIDAY")
                    {
                        // -- dont consider weekends
                        prmDate = prmDate.AddDays(3);
                    }
                    else
                    {
                        prmDate = prmDate.AddDays(1);
                    }
                }

                return prmDate;
            }
            catch (Exception)
            {

                return DateTime.Now.Date;
            }


        }

        /// <summary>
        /// Converts a time from one time zone to another based on time zone identifiers
        /// </summary>
        /// <param name="ParamTimezoneID">Timezone ID</param>
        /// <returns>Current Datetime</returns>
        public static DateTime? GetTimeByTimezoneID(string ParamTimezoneID)
        {
            try
            {
                DateTime dt = DateTime.Now;
                if (!string.IsNullOrEmpty(ParamTimezoneID))
                {
                    return TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dt, TimeZoneInfo.Local.Id, ParamTimezoneID.Trim());
                }
                else
                {
                    return null;
                }
                
            }
            catch (Exception)
            {

                return null;
            }
        }

        /// <summary>
        /// Returns a sorted collection of all the time zones about which information is available on the local system.
        /// </summary>
        /// <returns>A read-only collection of TimeZoneInfo objects.</returns>
        public static System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> GetAllSystemTimezone()
        {
            try
            {
                return TimeZoneInfo.GetSystemTimeZones();
            }
            catch (Exception)
            {

                return null;
            }
            
        }

        /// <summary>
        /// Check for valid date
        /// </summary>
        /// <param name="dateStr">Date</param>
        /// <param name="dt">Valid return date</param>
        /// <returns></returns>
        public static bool IsDate(string dateStr, out DateTime dt)
        {
            dt = DateTime.MinValue;
            bool validDate = false;

            try
            {
                //validDate = DateTime.TryParseExact(dateStr, format, CultureInfo.CurrentCulture, DateTimeStyles.None, out dt);
                validDate = DateTime.TryParse(dateStr, out dt);
            }
            catch
            {
                return false;
            }

            return validDate;
        }

        /// <summary>
        /// Get the Last Date of a given month
        /// </summary>
        /// <param name="dt">Date</param>
        /// <returns>Date</returns>
        public static DateTime? GetLastDateOfTheGivenMonth(DateTime dt)
        {
            try
            {
                DateTime dtmDate = new DateTime(dt.Year, dt.Month, 1);
                return dtmDate.AddMonths(1).AddDays(-1);
            }
            catch (Exception)
            {
                return null;
            }

        
        }
    }
}
